package com.system.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.system.entity.User;
import com.system.mapper.UserMapper;
import com.system.service.UserService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author hujunwei
 * @since 2021-11-10
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public void addUser() {
        User user = new User();
        user.setAge(18);
        user.setName("胡俊伟");
        user.setBirthday("2021-11-22");
        baseMapper.insert(user);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public void addUserException() {
        User user = new User();
        user.setAge(18);
        user.setName("胡俊伟Exception");
        user.setBirthday("2021-11-22");
        baseMapper.insert(user);
        throw new RuntimeException("新增用户胡俊伟异常!");
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public void addUserNew() {
        User user = new User();
        user.setAge(18);
        user.setName("胡俊伟");
        user.setBirthday("2021-11-22");
        baseMapper.insert(user);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public void addUserExceptionNew() {
        User user = new User();
        user.setAge(18);
        user.setName("胡俊伟Exception");
        user.setBirthday("2021-11-22");
        baseMapper.insert(user);
        throw new RuntimeException("新增用户胡俊伟异常!");
    }
}
